package com.ltc.cdr.tests;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import com.ltc.cdr.domain.CdrVolume;

public class readindirector {

	/**
	   * Returns a list of files 
	   * @param file
	   * @return
	   */
	  public  ArrayList<String> findFiles(File file)
	  {
	    // create an empty array list...
	    ArrayList<String> arr = new ArrayList<String>();
	    
	    // pass it into the recursive method
	    findFiles(file, arr);
	    return arr;
	  }
	  
	  /**
	   * Recursive helper method includes an array list as an argument.
	   * Filenames are added to the array list as they are found.
	   * @param file
	   * @param list
	   */
	  private  void findFiles(File file, ArrayList<String> list)
	  {
	    if (!file.isDirectory())
	    {
	        list.add(file.getAbsolutePath());
	    }
	    else
	    {
	      // recursively search the subdirectory
	      for (File f : file.listFiles())
	      {
	        findFiles(f, list);
	      }
	    }
	    
	    
	  }
	
	
	  // return list of cdr volum
	  
	// return list of cdr volum
		  public List<CdrVolume> txtread(String path) {
			  List<CdrVolume> cdr=null;
			  if(path!=null && path.contains("ConvertData4g")){
				 cdr= txtread4G(path);
			  }else if(path!=null && path.contains("ConvertData3g")){
				  cdr = txtread3G(path);
			  }else if(path!=null && path.contains("ConvertData2g")){
				  cdr = txtread2G(path);
			  }
			  return cdr;
		  }
	  
	private List<CdrVolume> txtread2G(String path) {
		File file = new File(path);
		List<CdrVolume> cdr1 = new ArrayList<CdrVolume>();
		
		try {
			BufferedReader br = new BufferedReader(new FileReader(file));
			String line;
			
			int nb=0;
			while ((line = br.readLine()) != null) {
					
					String[] arr = line.split(",");

					String tmp = arr[14];
					String[] strVol = tmp.split(":");
					String volupl = strVol[3].replaceAll(" VolDnLk", ""); // voluplnk
					String voldnl = strVol[4].replaceAll(" ChangeCon", ""); // voldnlnk
					String cht = strVol[6].replaceAll("}}", ""); // changetime
			
					CdrVolume cdr = new CdrVolume();
					
					String filenam = path.substring(path.lastIndexOf("ConvertData")+14,path.length());
					cdr.setFilenameid(nb+"_"+filenam);
					cdr.setServedmsidsn(arr[26].toString());
					cdr.setServedimsi(arr[3].toString());
					cdr.setServedimei(arr[4].toString());
					cdr.setVoluplk(Integer.parseInt(volupl));
					cdr.setVoldnlk(Integer.parseInt(voldnl));
					cdr.setChangetime(cht);
					cdr.setRecordopeningtime(arr[15].toString());
					cdr.setDurations(Integer.parseInt(arr[16]));
					cdr.setFilename(path);
					Calendar calendar = Calendar.getInstance();
					java.util.Date now = calendar.getTime();
					cdr.setInsertData(now);
					cdr1.add(cdr);
					nb++;
			}

			br.close();
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		
		return cdr1;
		}

	private List<CdrVolume> txtread4G(String path) {
		File file = new File(path);
		List<CdrVolume> cdr1 = new ArrayList<CdrVolume>();
		
		String test="";
		try {
			BufferedReader br = new BufferedReader(new FileReader(file));
			String line;
			
			int i=0;
			while ((line = br.readLine()) != null) {
				
					String[] arr = line.split(",");

					String tmp = arr[14];
					String[] strVol = tmp.split(":");
					String volupl = strVol[1].replaceAll(" VolDnLk", ""); // voluplnk
					String voldnl = strVol[2].replaceAll(" ChangeCon", ""); // voldnlnk
					String cht = strVol[4].replaceAll(" qoSInformationNeg", ""); // changetime

					// String a =
					// arr[26]+","+arr[2]+","+arr[3]+","+volupl+","+voldnl+","+cht+","+arr[15]+","+arr[16];
					
					CdrVolume cdr = new CdrVolume();
					String filenam = path.substring(path.lastIndexOf("ConvertData")+14,path.length());
					cdr.setFilenameid(i+"_"+filenam);
					cdr.setServedmsidsn(arr[26].toString());
					cdr.setServedimsi(arr[2].toString());
					cdr.setServedimei(arr[9].toString());
					cdr.setVoluplk(Integer.parseInt(volupl));
					cdr.setVoldnlk(Integer.parseInt(voldnl));
					cdr.setChangetime(cht);
					cdr.setRecordopeningtime(arr[15].toString());
					cdr.setDurations(Integer.parseInt(arr[16]));
					cdr.setFilename(path);
					Calendar calendar = Calendar.getInstance();
					java.util.Date now = calendar.getTime();
					cdr.setInsertData(now);
					cdr1.add(cdr);
				
					i++;
			}

			br.close();
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		
		return cdr1;
	}

	public List<CdrVolume> txtread3G(String path) {
		File file = new File(path);
		List<CdrVolume> cdr1 = new ArrayList<CdrVolume>();
		try {
			BufferedReader br = new BufferedReader(new FileReader(file));
			String line;

			int k=0;

			while ((line = br.readLine()) != null) {
				

					String[] arr = line.split(",");

					String tmp = arr[14];
					String[] strVol = tmp.split(":");
					String volupl = strVol[3].replaceAll(" VolDnLk", ""); // voluplnk
					String voldnl = strVol[4].replaceAll(" ChangeCon", ""); // voldnlnk
					String cht = strVol[6].replaceAll("}}", ""); // changetime

					// String a =
					// arr[26]+","+arr[2]+","+arr[3]+","+volupl+","+voldnl+","+cht+","+arr[15]+","+arr[16];
					
					CdrVolume cdr = new CdrVolume();
					String filenam = path.substring(path.lastIndexOf("ConvertData")+14,path.length());
					cdr.setFilenameid(k+"_"+filenam);
					cdr.setServedmsidsn(arr[26].toString());
					cdr.setServedimsi(arr[2].toString());
					cdr.setServedimei(arr[3].toString());
					cdr.setVoluplk(Integer.parseInt(volupl));
					cdr.setVoldnlk(Integer.parseInt(voldnl));
					cdr.setChangetime(cht);
					cdr.setRecordopeningtime(arr[15].toString());
					cdr.setDurations(Integer.parseInt(arr[16]));
					cdr.setFilename(path);
					Calendar calendar = Calendar.getInstance();
					java.util.Date now = calendar.getTime();
					cdr.setInsertData(now);
					cdr1.add(cdr);
					
					k++;

			}

			br.close();
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		
		return cdr1;

	}

}
